package org.ns.summer.classpath.scan;

import java.lang.annotation.Annotation;

/**
 * 扫描文件对象
 * 
 * @author 张星宇
 *
 * @email m.vip@qq.com
 *
 */
public class ScanFile {

	private String rootPath; // 跟路径
	private String rootSuffix;
	private String filePath; // 文件路径
	private String className; // 如果是一个class 则可以直接得到className
	private String absolutePath; // 文件的绝对路径
	private ScanClassInfo scanClassInfo;

	public ScanFile(String absolutePath) {
		this(ScanFileUtils.getFileString(absolutePath), ScanFileUtils.getFilePathString(absolutePath),
				ScanFileUtils.getFileAbsoluteString(absolutePath));
	}

	public ScanFile(String rootPath, String filePath, String absolutePath) {
		super();
		filePath = filePath.replace("\\", "/");
		this.rootPath = rootPath;
		this.filePath = filePath;
		this.absolutePath = absolutePath;
		int suffixIndex = rootPath.lastIndexOf(".");
		if (suffixIndex != -1) {
			this.rootSuffix = rootPath.substring(suffixIndex + 1);
		}
		if (filePath.endsWith(".class")) {
			className = filePath.replace("/", ".").substring(0, filePath.length() - ".class".length());
		}
	}

	/**
	 * 文件跟路径
	 * 
	 * @return
	 */
	public String getRootPath() {
		return rootPath;
	}

	/**
	 * 文件后缀
	 * 
	 * @return
	 */
	public String getRootSuffix() {
		return rootSuffix;
	}

	/**
	 * 文件路径
	 * 
	 * @return
	 */
	public String getFilePath() {
		return filePath;
	}

	/**
	 * 类名称 如果是类则有
	 * 
	 * @return
	 */
	public String getClassName() {
		return className;
	}

	/**
	 * 文件的绝对路径
	 * 
	 * @return
	 */
	public String getAbsolutePath() {
		return absolutePath;
	}

	@Override
	public String toString() {
		StringBuilder text = new StringBuilder();
		text.append("{");
		text.append("\"rootPath\":").append("\"").append(rootPath).append("\"");
		if (className != null) {
			text.append(",\"className\":").append("\"").append(className).append("\"");
		} else {
			text.append(",\"filePath\":").append("\"").append(filePath).append("\"");
		}
		text.append("}");
		return text.toString();
	}

	// 设置class
	void setClassName(String className) {
		if (className != null) {
			this.className = className;
		}
	}

	void setScanClassInfo(ScanClassInfo scanClassInfo) {
		this.scanClassInfo = scanClassInfo;
	}

	public ScanClassInfo getScanClassInfo() {
		return scanClassInfo;
	}

	public boolean isAnnotation(Class<? extends Annotation> annType) {
		return scanClassInfo != null && scanClassInfo.isAnnotation(annType);
	}
}
